package com.cac.demo.newCode.standardAssort.service.Impl;

import com.cac.demo.newCode.standardAssort.dto.MsAoBzjBillDTO;
import com.cac.demo.newCode.standardAssort.mapper.MsAoBzjBillMapper;
import com.cac.demo.newCode.standardAssort.service.MsAoBzjBillService;
import com.cac.demo.newCode.standardAssort.vo.MsAoBzjBillVO;
import com.cac.demo.newCode.standardAssort.vo.WmsItemInventoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zty
 * @description 标准件处理
 * @date 2024/10/25
 */
@Service
public class MsAoBzjBillServiceImpl implements MsAoBzjBillService {

    @Resource
    private MsAoBzjBillMapper msAoBzjBillMapper;

    /**
     * 根据工作包AOid查询物料配套信息列表
     *
     * @param msTaskAoId
     * @return
     */

    @Override
    public List<MsAoBzjBillVO> selectWithLackItemQty(MsAoBzjBillDTO msAoBzjBillDTO) {
        //参数校验
        if (msAoBzjBillDTO.getMsTaskAoId() == null || msAoBzjBillDTO.getMsTaskAoId().isEmpty()) {
            return new ArrayList<>();
        }
        try {
            //查询物料配套信息列表
            List<MsAoBzjBillVO> msAoBzjBillVOS = msAoBzjBillMapper.selectWithLackItemQty(msAoBzjBillDTO.getMsTaskAoId());
            //遍历获取
            for (MsAoBzjBillVO msAoBzjBillVO : msAoBzjBillVOS) {
                handleStandardPiece(msAoBzjBillVO, msAoBzjBillDTO);
            }

            if (msAoBzjBillVOS == null) {
                msAoBzjBillVOS = new ArrayList<>();
            }
            return msAoBzjBillVOS;
        } catch (Exception e) {
            e.getMessage();
            return new ArrayList<>();
        }
    }

    @Transactional
     void handleStandardPiece(MsAoBzjBillVO msAoBzjBillVO, MsAoBzjBillDTO msAoBzjBillDTO) {
        if (msAoBzjBillVO == null || msAoBzjBillDTO == null) {
            throw new IllegalArgumentException("输入参数不能为空");
        }

        int lackItemQty = msAoBzjBillVO.getLackItemQty();
        if (lackItemQty <= 0) {
            throw new IllegalArgumentException("标准件缺件数量不能为负数");
        }

        /**
         * 已配套数量
         */
        int alreadyPreAmount = 0;
        /**
         * 未配套数量
         */
        int noPreMatchAmount = lackItemQty;
        /**
         *  箱子已经分配的数量，标准件只会有一个箱子
         */
        int workbinAlreadyUseAmount = 0;
        /**
         * 查询台账数量
         */
        List<WmsItemInventoryVO> wmsItemInventoryVOS = msAoBzjBillMapper.selectWmsItenQty(msAoBzjBillVO, msAoBzjBillDTO.getDepartCode());

        Map<String, Integer> workbinAlreadyUseAmountMap = new HashMap();

        for (WmsItemInventoryVO wmsItemInventoryVO : wmsItemInventoryVOS) {
            if (wmsItemInventoryVO == null) {
                continue;
            }
            String wmsItemInventoryId = wmsItemInventoryVO.getWmsItemInventoryId();
            workbinAlreadyUseAmount = workbinAlreadyUseAmountMap.computeIfAbsent(wmsItemInventoryId, id -> msAoBzjBillMapper.getWorkbinAlreadyUseAmount(id));

            if (noPreMatchAmount == 0) {
                // 当前标准件未配套数量为0，说明配套完成
                break;
            }
            //判断当前台账是否已经分配完
            int currentOnhandQty = wmsItemInventoryVO.getCurrentOnhandQty();
            if (currentOnhandQty - workbinAlreadyUseAmount <= 0) {
                continue;
            }
            //配套数量是否小于等于当前库存
            int reqMatchQty;
            if (noPreMatchAmount <= currentOnhandQty - workbinAlreadyUseAmount) {
                // 库存够用
                alreadyPreAmount += noPreMatchAmount;  //已经配套的数量
                reqMatchQty = noPreMatchAmount;  //设置需要分配的数量
                noPreMatchAmount = 0;  //未配套的数量
            } else {
                // 库存不够用
                alreadyPreAmount += currentOnhandQty - workbinAlreadyUseAmount;  //已经配套的数量
                reqMatchQty = currentOnhandQty - workbinAlreadyUseAmount;
                noPreMatchAmount -= (currentOnhandQty - workbinAlreadyUseAmount);
            }
            //插入台账临时表及配套信息（结果表）
           // msAoBzjBillMapper.insertSortItemInventoryTemp(msAoBzjBillVO, wmsItemInventoryVO, reqMatchQty);
            //msAoBzjBillMapper.insertAoMatchInfo(msAoBzjBillVO, wmsItemInventoryVO, msAoBzjBillDTO, reqMatchQty);

            if (noPreMatchAmount == lackItemQty) {
                // 当前标准件未找到任何库存
              //  msAoBzjBillMapper.insertAoMatchInfoForNoInventory(msAoBzjBillVO, msAoBzjBillDTO);
            }
        }
        //更新标准件是否缺件
        msAoBzjBillMapper.updateAoMatchInfoState(msAoBzjBillVO.getMsAoMatchBillId());
        msAoBzjBillMapper.updateAoMatchInfoStateForZeroMatchQty(msAoBzjBillVO.getMsAoMatchBillId());

    }
}
